package com.atg.ai_agent.rag.config;


import com.atg.ai_agent.rag.LoveAppDocumentLoader;
import com.atg.ai_agent.rag.MyKeywordEnricher;
import com.atg.ai_agent.rag.MyTokenTextSplitter;
import jakarta.annotation.Resource;
import lombok.Locked;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.List;

/*
author: atg
time: 2025/9/19 12:39
*/

// 本地向量存储
@Configuration
public class LoveAppVectorStoreConfig {
    @Resource
    private LoveAppDocumentLoader loveAppDocumentLoader;

    @Resource
    private MyTokenTextSplitter myTokenTextSplitter;

    @Resource
    private MyKeywordEnricher myKeywordEnricher;


    @Bean
    VectorStore loveAppVectorStore(EmbeddingModel dashscopeEmbeddingModel) throws IOException {
        // 选择和合适的向量存储模型
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(dashscopeEmbeddingModel)
                .build();
    // 加载文档
        List<Document> documents = loveAppDocumentLoader.loadMarkdowns();

        // 切分文档 根据设置的参数值自主切分  缺点：无法保证切分后文档顺序不， 容易破坏文档的语义的完整性
//        List<Document> splitDocuments = myTokenTextSplitter.splitCustomized(documents);
//        simpleVectorStore.add(splitDocuments);

        // 自动补充原信息
        List<Document> enrichDocuments = myKeywordEnricher.enrichDocuments(documents);
        simpleVectorStore.add(enrichDocuments);
//        simpleVectorStore.add(documents);
        return simpleVectorStore;

    }
}
